
cc.Class({
    extends: cc.Component,

    properties: {
        dragModel:{
            default:null,
            type:cc.Prefab
        },

        showSprite:{
            default:null,
            type:cc.Sprite
        }
    },

    onLoad () {
        // this.node.on(cc.Node.EventType.TOUCH_START,this._onDragItemMgrTouchBegin.bind(this),this);
        // this.node.on(cc.Node.EventType.TOUCH_MOVE,this._onDragItemMgrTouchMoving.bind(this),this);
        // this.node.on(cc.Node.EventType.TOUCH_END,this._onDragItemMgrTouchEnd.bind(this),this);
        // this.node.on(cc.Node.EventType.TOUCH_CANCEL,this._onDragItemMgrTouchCancel.bind(this),this);
    },

    start () {

    },

    _onDragItemMgrTouchBegin:function (event) {
        if(!cc.gg.tpgMgr.pokerStackDragable){
            return
        }

        let touchPos = event.getLocation()


        let tmpBoundingBox = this.node.getBoundingBoxToWorld()

        if (tmpBoundingBox.contains(touchPos)) {
            this.cloneDragItemToDrag(this.dragModel)
        }
    },

    _onDragItemMgrTouchMoving:function (event) {
        if(!cc.gg.tpgMgr.pokerStackDragable){
            return
        }

        if(!this.draggingSymbol){
            return
        }

        let delta = event.getDelta()

        this.draggingSymbol.x += delta.x
        this.draggingSymbol.y += delta.y
    },

    _onDragItemMgrTouchEnd:function (event) {
        if(!cc.gg.tpgMgr.pokerStackDragable){
            return
        }

        let worldPos = this.draggingSymbol.parent.convertToWorldSpaceAR(this.draggingSymbol.position)
        cc.gg.emitEvent("player_grab_poker",cc.gg.dataMgr.playerId,worldPos)

        this.clearDragItemToDrag()
    },

    _onDragItemMgrTouchCancel:function (event) {
        if(!cc.gg.tpgMgr.pokerStackDragable){
            return
        }
        let worldPos = this.draggingSymbol.parent.convertToWorldSpaceAR(this.draggingSymbol.position)
        cc.gg.emitEvent("player_grab_poker",cc.gg.dataMgr.playerId,worldPos)

        this.clearDragItemToDrag()
    },

    cloneDragItemToDrag:function (dragNode) {
        let newDragNode = cc.instantiate(dragNode)
        newDragNode.active = true

        this.node.addChild(newDragNode)
        this.draggingSymbol = newDragNode
    },

    clearDragItemToDrag:function () {
        if(!this.draggingSymbol)
            return

        if(!cc.isValid(this.draggingSymbol,true))
            return

        this.draggingSymbol.destroy()
        this.draggingSymbol = null
    },
});
